package com.tianbing.adlottery.controller.servantadmin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jubao.ejb.dao.CouponDao;
import com.jubao.ejb.entity.Coupon;
import com.tianbing.adlottery.util.ControllerFormBean;

public class EditCouponProfileController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditCouponProfileController.class.getName());


	private CouponDao couponDao;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		ControllerFormBean fb = new ControllerFormBean();
		long cpsid = ServletRequestUtils.getLongParameter(request, "cpsid", -1);
		Coupon coupon = couponDao.findCoupon(cpsid);
		fb.setCoupon(coupon);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		long cpsid = ServletRequestUtils.getLongParameter(request, "cpsid", -1);
		ModelAndView mv = new ModelAndView(this.getSuccessView()+"?cpsid="+cpsid);
		ControllerFormBean form = (ControllerFormBean) cmd;

		Coupon coupon = form.getCoupon();
		couponDao.saveCoupon(coupon);
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();
		return model;
	}

	public static void setLogger(Logger logger) {
		EditCouponProfileController.logger = logger;
	}

	public void setCouponDao(CouponDao couponDao) {
		this.couponDao = couponDao;
	}

}
